import { _decorator, Component, Layout, Node, ScrollView, Sprite, SpriteFrame } from 'cc';
import { UIBase } from '../../FrameWork3.8.6/FrameWork/UIFramework/UIBase';
import { PlayerPropertyBase } from './PlayerPropertyBase';
import { PlayerSkillDtMgr } from '../../FrameWork3.8.6/FrameWork/Data/DataBaseMgr';
import { DataMgr } from '../../FrameWork3.8.6/FrameWork/Data/DataMgr';
import { Emitter } from '../../FrameWork3.8.6/FrameWork/Common/Emitter';
import { PlayerSkillDt } from '../../FrameWork3.8.6/FrameWork/Common/CommonInterface';
import { ResMgr } from '../../FrameWork3.8.6/FrameWork/Res/ResMgr';
import { BundleConfig } from '../../FrameWork3.8.6/FrameWork/NeedToChange/BundleConfig';
import { GridButton } from '../Game/Object/GridButton';
import { BagManager } from '../Game/Manager/BagManager';
const { ccclass, property } = _decorator;

@ccclass('SkillPanel')
export class SkillPanel extends PlayerPropertyBase {
    //接受技能数据管理者
    private _skillDtMgt:PlayerSkillDtMgr=null;

    //接受技能数据数组
    private _arrSkillDt:PlayerSkillDt[]=[];
    //管理当前网格的容器，判断是否有相同的，如果有则在它下面的文字+1
    private _mapGrid:Map<number,GridButton>=new Map();
    initData(): void {
        this._skillDtMgt=DataMgr.instance.getMgr<PlayerSkillDtMgr>("PlayerSkillDtMgr");
        //注册更新装备的方法
        Emitter.instance.on("refreshEquipmentDt",this.refreshEquipmentDt,this);
        //this.refreshSkillGrid();
    }

    //在每一次打开该界面时读取背包信息技能表当中的数据
    onEnter(...rest: any[]): void {
        this.refreshSkillGrid();
        this.refreshEquipmentDt();
    }

    //更新网格的方法
    refreshSkillGrid(){
        //拿到数据存储起来
        this._arrSkillDt=BagManager.instance.getBagDt<PlayerSkillDt>("Skill",false);
        //先清空现有网格
        let content=this.getComp<ScrollView>("_ScrollView","ScrollView").content;
        content.removeAllChildren();
        if(!this._arrSkillDt){
            return;
        }
        for(let skillDt of this._arrSkillDt){
            //在实例化对象之前先判断是否有一样的
            let gridButton=this._mapGrid.get(skillDt.id);
            if(gridButton){
                //说明重复了
                gridButton.num+=1;
                gridButton.onInit(skillDt);
                continue;
            }
            //实例化对象并且赋值他的图片与名字
            let gridNode=this._gridBtnNodePool.alloc();
            gridNode.parent=content;
            let gridTs=gridNode.getComponent(GridButton);
            this._mapGrid.set(skillDt.id,gridTs);
            gridTs.onInit(skillDt);
        }
        for(let value of this._mapGrid.values()){
            //注册点击事件
            value.node.on("click",()=>{
                this.openDetailsPanel(value.curPropertyDt,value.num);
            },this);
        }
    }

    //更新已装备数据的方法
    refreshEquipmentDt(){
        //先获取背包数据的已经装备的数据列表
        let arrEquipDt=BagManager.instance.getBagDt("Skill",true);
        if(!arrEquipDt){
            return;
        }
        //遍历数组依次设置给按钮的图片并且给按钮注册点击事件
        for(let equipDt of arrEquipDt){
            //先通过id找到对应节点
            let equiBtn=this.getNode("_EquipBtn"+equipDt.id.toString().charAt(3));
            //设置对应图片
            equiBtn.getComponent(Sprite).spriteFrame=ResMgr.instance.getRes<SpriteFrame>(BundleConfig.Img.name,equipDt.iconName);
            equiBtn.targetOff(equiBtn);
            //注册点击事件
            equiBtn.on("click",()=>{
                this.openEquipDetailsPanel(equipDt);
            },equiBtn)
        }
    }
    
    //关闭时清空
    onExit(...rest: any[]): void {
        this._mapGrid.clear();
    }
    
}


